package com.dadaban.controller;

import com.dadaban.UserConstant;
import com.dadaban.UserTool;
import com.dadaban.model.User;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liu_yan on 2018/9/16.
 */
@RestController
@RequestMapping(value="/user")
public class UserController {
    private Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserRepository userRepository;

    /**
     * 查询所有用户信息
     * @return
     */
    @GetMapping(value = "/getAll")
    public Map<String, Object> getAllUsers(){
    	LOGGER.info("getAllUsers 查询所有用户信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	List<User> userList = userRepository.findAll();
    	if(userList == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "查询所有用户信息出错");
    	}
    	resultMap.put("userAll", userList);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
        return resultMap;
    }
    
    /**
     * 根据usrUid查询用户信息
     * @return
     */
    @GetMapping(value = "/getUsrInfo/{usrUid}")
    public Map<String, Object> getUsersInfo(@PathVariable("usrUid") String usrUid){
    	LOGGER.info("getUsersInfo 根据usrUid查询用户信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	User user = new User();
        user.setUsrUid(usrUid);
        user.setSts("A");
    	Example<User> example = Example.of(user);
    	if(userRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据usrUid查询用户信息出错");
        	return resultMap;
    	}
    	resultMap.put("userInfo", userRepository.findOne(example));
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 新增用户信息
     * @return
     */
    @PostMapping(value = "/addUser")
    public Map<String, Object> addUser(@RequestParam(value = "weChtUId", required = true) String weChtUId,
    		@RequestParam(value = "usrNam", required = true) String usrNam,
    		@RequestParam(value = "usrPsw", required = true) String usrPsw,
    		@RequestParam(value = "usrTyp", required = true) String usrTyp,
    		@RequestParam(value = "cmbUid", required = false) String cmbUid,
    		@RequestParam(value = "usrNckNam", required = false) String usrNckNam,
    		@RequestParam(value = "usrSex", required = false, defaultValue="N") String usrSex,
    		@RequestParam(value = "usrEml", required = false) String usrEml,
    		@RequestParam(value = "usrMbl", required = false) String usrMbl){
        LOGGER.info("addUser 新增用户信息");
        /*
         * 必填字段校验
         */
        Map<String, Object> resultMap = new HashMap<String, Object>();
    	resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
    	resultMap.put(UserConstant.ErrorMsg, "新增用户信息出错");
        if(weChtUId == null || "".equals(weChtUId.trim())){
        	LOGGER.error("addUser 新增用户信息出错，缺少用户微信号weChtUId");
        	return resultMap;
        }
        if(usrNam == null || "".equals(usrNam.trim())){
        	LOGGER.error("addUser 新增用户信息出错，缺少用户姓名usrNam");
        	return resultMap;
        }
        if(usrPsw == null || "".equals(usrPsw.trim())){
        	LOGGER.error("addUser 新增用户信息出错，缺少用户密码usrPsw");
        	return resultMap;
        }
        if(usrTyp == null || "".equals(usrTyp.trim())){
        	LOGGER.error("addUser 新增用户信息出错，缺少用户类型usrTyp");
        	return resultMap;
        }
        /*
         * 新增赋值
         */
        User user = new User();
        user.setCmbUid(cmbUid);
        user.setWeChtUId(weChtUId);
        user.setUsrNam(usrNam);
        user.setUsrNckNam(usrNckNam);
        user.setUsrPsw(usrPsw);
        user.setUsrSex(usrSex);
        user.setUsrTyp(usrTyp);
        user.setUsrEml(usrEml);
        user.setUsrMbl(usrMbl);
        user.setSts("A");
        Date d = new Date();
        SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
        user.setCrtDte(ddf.format(d));
        user.setCrtTim(tdf.format(d));
        userRepository.save(user);
        
        resultMap.put("userInfo", user);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 根据usrUid修改用户信息
     * @return
     */
    @PutMapping(value = "/updateUser/{usrUid}")
    public Map<String, Object> updateUser(@PathVariable("usrUid") String usrUid,
    		@RequestParam(value = "weChtUId", required = false) String weChtUId,
    		@RequestParam(value = "usrNam", required = false) String usrNam,
    		@RequestParam(value = "usrPsw", required = false) String usrPsw,
    		@RequestParam(value = "usrTyp", required = false) String usrTyp,
    		@RequestParam(value = "cmbUid", required = false) String cmbUid,
    		@RequestParam(value = "usrNckNam", required = false) String usrNckNam,
    		@RequestParam(value = "usrSex", required = false, defaultValue="N") String usrSex,
    		@RequestParam(value = "usrEml", required = false) String usrEml,
    		@RequestParam(value = "usrMbl", required = false) String usrMbl){
    	LOGGER.info("updateUser 根据usrUid修改用户信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	/*
    	 * 校验
    	 */
    	User user = new User();
    	user.setUsrUid(usrUid);
    	user.setSts("A");
    	Example<User> example = Example.of(user);
    	if(userRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据usrUid修改用户信息出错");
        	return resultMap;
    	}
    	User source = userRepository.findOne(example).get();
    	
    	/*
    	 * 用户数据更新
    	 */
    	user.setCmbUid(cmbUid);
    	user.setWeChtUId(weChtUId);
    	user.setUsrNam(usrNam);
    	user.setUsrNckNam(usrNckNam);
    	user.setUsrPsw(usrPsw);
    	user.setUsrSex(usrSex);
    	user.setUsrTyp(usrTyp);
    	user.setUsrEml(usrEml);
    	user.setUsrMbl(usrMbl);
    	Date d = new Date();
    	SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
    	user.setRecDte(ddf.format(d));
    	user.setRecTim(tdf.format(d));
        UserTool.copyNullProperties(source, user);
        userRepository.save(user);
        
        resultMap.put("userInfo", user);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
    /**
     * 根据usrUid删除用户信息
     * @return
     */
    @DeleteMapping(value = "/deleteUser/{usrUid}")
    public Map<String, Object> deleteUser(@PathVariable("usrUid") String usrUid){
    	LOGGER.info("deleteUser 根据usrUid删除用户信息");
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	/*
    	 * 校验
    	 */
    	User user = new User();
    	user.setUsrUid(usrUid);
    	user.setSts("A");
    	Example<User> example = Example.of(user);
    	if(userRepository.findOne(example).orElse(null) == null){
    		resultMap.put(UserConstant.ErrorNo, UserConstant.FAIL);
        	resultMap.put(UserConstant.ErrorMsg, "根据usrUid删除用户信息出错");
        	return resultMap;
    	}
    	User source = userRepository.findOne(example).get();
    	
    	user.setSts("C");
    	Date d = new Date();
    	SimpleDateFormat ddf = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat tdf = new SimpleDateFormat("kkmmss");
    	user.setRecDte(ddf.format(d));
    	user.setRecTim(tdf.format(d));
        UserTool.copyNullProperties(source, user);
        userRepository.save(user);
        
        resultMap.put("userInfo", user);
    	resultMap.put(UserConstant.ErrorNo, UserConstant.SUCCESS);
    	resultMap.put(UserConstant.ErrorMsg, "");
    	return resultMap;
    }
    
}
